package com.gview.example.common.viewDynamic;


import com.gonsin.gview.annotations.GContext;
import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.board.GBoard;
import com.gonsin.gview.annotations.board.GButton;
import com.gonsin.gview.annotations.board.form.GTextInput;
import com.gonsin.gview.annotations.board.table.*;
import com.gonsin.gview.annotations.data.GTableData;
import com.gonsin.gview.annotations.dynamic.GDynamicView;
import com.gonsin.gview.annotations.enums.ButtonAction;
import com.gonsin.gview.annotations.enums.ColumnType;
import com.gonsin.gview.model.dynamic.DynamicButton;
import com.gonsin.gview.model.dynamic.DynamicProp;
import com.gonsin.gview.model.dynamic.data.DynamicTableData;
import com.gonsin.gview.model.dynamic.form.DynamicTextInput;
import com.gonsin.gview.model.dynamic.table.*;

import java.util.Arrays;
import java.util.List;

@GDynamicView(viewName = "BookView2")
public class BookView implements DynamicTableView {

    @Override
    public String title(GContext context) {
        return "图书管理";
    }

    @Override
    public DynamicTableData tableData(GContext context) {
        return DynamicTableData.fromDataSource("searchBook").keyName("bookKey").build();
    }

    @Override
    public DynamicHeader header(GContext context) {
        return DynamicHeader.builder()
                .buttons(Arrays.asList(
                        DynamicButton.builder().name("新添加").action(ButtonAction.VIEW_BY_DIALOG).view("AddBook").build(),
                        DynamicButton.builder().name("删除").action(ButtonAction.BATCH).actionName("batchDeleteBook").build())
                )
                .searchBar(DynamicSearchBar.builder().filters(Arrays.asList(
                        DynamicTextInput.builder().keyName("bookName").name("查找").placeholder("输入图书名字").build()
                )).build())
                .build();
    }

    @Override
    public List<DynamicColumn> columns(GContext context) {
        return Arrays.asList(
                DynamicColumn.builder().name("名字").keyName("bookName").columnType(ColumnType.TEXT).build(),
                DynamicColumn.builder().name("作者").keyName("author").columnType(ColumnType.TEXT).build(),
                DynamicColumn.builder().name("价格").value("{{item.price}} ￥").columnType(ColumnType.TEXT).build(),
                DynamicColumn.builder().name("操作").columnType(ColumnType.BUTTONS).buttons(Arrays.asList(
                        DynamicButton.builder().name("编辑").action(ButtonAction.VIEW_BY_DIALOG).view("UpdateBook").props(Arrays.asList(
                                DynamicProp.builder().keyName("bookKey").valueName("{{item.bookKey}}").build()
                        )).build(),
                        DynamicButton.builder().name("删除").action(ButtonAction.CONFIRM).actionName("deleteBook").confirmMessage("是否确认删除这本书？").props(Arrays.asList(
                                DynamicProp.builder().keyName("bookKey").valueName("{{item.bookKey}}").build()
                        )).build()
                )).build()
        );
    }
}
